package itsm.isperp.module.controller.sparepartOld;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseProcessController;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.sparepart.SptSparepartIn;
import itsm.isperp.module.service.app.AppUserService;
import itsm.isperp.module.service.sparepart.SptSparepartInService;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 备件管理控制器
 * 
 * @author lizx
 * @date 2013-9-12
 */
@Controller
@RequestMapping("sparepart/sparepartIn/old")
public class SptSparepartInOldController extends
		BaseProcessController<SptSparepartIn> {

	@Autowired
	protected SptSparepartInService sptSparepartInService;

	@Override
	public SptSparepartInService getService() {
		return sptSparepartInService;
	}

	@Override
	public String getPageTitle() {
		return "备件管理";
	}

	@Override
	public String getProceessKey() {
		return "sparepartInProcess";
	}

	@RequestMapping("edit")
	public ModelAndView edit(String id, HttpServletRequest req)
			throws Exception {
		ModelAndView mv = this.getModelAndView();

		SptSparepartIn model = null;

		AppUserService appUserService = ContextHolder
				.getSpringBean("appUserService");

		if (StringUtils.isEmpty(id)) {
			model = new SptSparepartIn();
			model.setId(HibernateUUIDGenerator.generate());
			model.setCreator(ContextHolder.getLoginUsername());

			model.setInstockDate(new Date());

		} else {
			model = this.getService().findOne(id);

		}

		mv.addObject("model", model);

		mv.addObject("creatorName",
				appUserService.findByUsername(model.getCreator()).getFullname());

		return mv;

	}

	@RequestMapping("inSptlist")
	@ResponseBody
	public PageResponse<Map<String, Object>> inSptlist(String sparepartId,
			JqGridRequest request, HttpServletRequest req) {
		return sptSparepartInService.findAllSptDetailByRequest(sparepartId,
				request);
	}

}
